package com.example.sens.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.example.sens.entity.Quiz;
import com.example.sens.entity.User;
import com.example.sens.entity.UserQuiz;
import com.example.sens.mapper.QuizMapper;
import com.example.sens.mapper.UserMapper;
import com.example.sens.mapper.UserQuizMapper;
import com.example.sens.service.UserQuizService;
import com.example.sens.utils.ExportExcelUtils;
import com.example.sens.utils.Response;
import com.example.sens.vo.UserQuizVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserQuizServiceImpl implements UserQuizService{

    @Resource
    private UserQuizMapper userQuizMapper;
    @Resource
    private QuizMapper quizMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public Page<UserQuiz> findAll(Page page,Long userId,String quizId) {
        List<UserQuiz> userQuizList = userQuizMapper.findAll(page,userId, Long.valueOf(quizId));
//        List<Long> quizIds = userQuizList.stream().map(p -> p.getQuizId()).collect(Collectors.toList());
//        List<Quiz> quizList = quizMapper.selectByQuizIds(quizIds);
//        userQuizList.stream().forEach(t -> {
//            Quiz quiz = quizList.stream().filter(p -> p.getId().equals(t.getQuizId())).findFirst().orElse(new Quiz());
//            t.setQuizTitle(quiz.getTitle());
//        });
        return page.setRecords(userQuizList);
    }

    @Override
    public Page<UserQuiz> findAllCustom(Page page) {
        List<UserQuiz> userQuizList = userQuizMapper.findAllCustom(page);
        List<Long> quizIds = userQuizList.stream().map(p -> p.getQuizId()).collect(Collectors.toList());
        List<Quiz> quizList = quizMapper.selectByQuizIds(quizIds);
        List<Long> userIds = userQuizList.stream().map(p -> p.getUserId()).collect(Collectors.toList());
        List<User> userList = userMapper.selectByUserIds(userIds);
        userQuizList.stream().forEach(t -> {
            Quiz quiz = quizList.stream().filter(p -> p.getId().equals(t.getQuizId())).findFirst().orElse(new Quiz());
            t.setQuizTitle(quiz.getTitle());
            User user = userList.stream().filter(p -> p.getUserId().equals(t.getUserId())).findFirst().orElse(new User());
            t.setUserName(user.getUserName());
        });
        return page.setRecords(userQuizList);
    }

    @Override
    public Response<String> exportExcel(UserQuizVo param) {
        List<UserQuiz> userQuizList = userQuizMapper.findAllCustom(new Page(0, 10000));
        List<Long> quizIds = userQuizList.stream().map(p -> p.getQuizId()).collect(Collectors.toList());
        List<Quiz> quizList = quizMapper.selectByQuizIds(quizIds);
        List<Long> userIds = userQuizList.stream().map(p -> p.getUserId()).collect(Collectors.toList());
        List<User> userList = userMapper.selectByUserIds(userIds);
        userQuizList.stream().forEach(t -> {
            Quiz quiz = quizList.stream().filter(p -> p.getId().equals(t.getQuizId())).findFirst().orElse(new Quiz());
            t.setQuizTitle(quiz.getTitle());
            User user = userList.stream().filter(p -> p.getUserId().equals(t.getUserId())).findFirst().orElse(new User());
            t.setUserName(user.getUserName());
        });
        List<Object[]> dataList = new ArrayList<>();
        userQuizList.stream().forEach(a -> {
            Object[] objects  = {
                    dataList.indexOf(a) + 1,
                    a.getUserName(),
                    a.getQuizTitle(),
                    a.getTotalScore(),
            };
            dataList.add(objects);
        });
        String title = "学生成绩表";
        String rowName[] = {"编号","学生姓名","实验名称","分数"};
        String layout[] = {"center", "center", "center", "center"};
        Response<String> response = ExportExcelUtils.excelPath(dataList, rowName, title, layout);
        response.setSuccess(true);
        return response;
    }

    @Override
    public Response update(UserQuiz userQuiz) {
        userQuizMapper.updateByPrimaryKeySelective(userQuiz);
        return Response.yes();
    }
}
